var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

var Remote = function (app) {
    this.app = app;
};

Remote.prototype.addGameRecord = function (msg, callback) {
    logger.info(`Remote addGameRecord ${JSON.stringify(msg)}`);
    let playerService = this.app.get("playerService");
    for(let playerScore of msg.playerScores) {
        playerService.findByUid(playerScore.uid, player => {
            if(!!player) {
                player.addPlayTimes();
                playerService.save(player);
                logger.debug(`player:${player.getUid()} playeTimes :${player.getPlayTimes()}`);
            }
        });
    }
    let gameRecordService = this.app.get("gameRecordService");
    gameRecordService.addGameRecord(msg.gameId, msg.tableId, msg.time, msg.playerScores, gameRecord =>{
        if(!!gameRecord) {
            logger.info(`Remote addGameRecord ${JSON.stringify(gameRecord)}`);
            callback({resultCode: 0, message: "ok", gameRecord: gameRecord.toJSON()});
            return;
        }
        callback({resultCode : 404, message: "插入gameRecord失败", gameRecord: {}});
    });
};
